import { defineCustomElement } from 'vue'
import HelloWorld from "../components/HelloWorld.ce.vue";

const componentConfig = {
  "hello-world": HelloWorld
};

export function registerWebComponent(tagName) {
  if (!tagName) {
    console.error(`请传入注册的标签名`);
    return false;
  }
  if (!componentConfig[tagName]) {
    console.error(`未找到${tagName}`);
    return false;
  }
  if (customElements.get(tagName)) {
    console.warn(`${tagName} 已注册`);
    return false;
  }

  try {
    const VueElement = defineCustomElement(componentConfig[tagName])
    customElements.define(tagName, VueElement)
  
    console.log(`${tagName} 注册成功`)
  } catch (error) {
    console.error(`${tagName} 注册失败: ${error}`)
  }
}

export function registerWebComponents() {
    const tagNames = Object.keys(componentConfig)
    tagNames.forEach(tagName => {
        registerWebComponent(tagName)
    })
    console.log(`全部注册成功`)
}

export function getComponents() {
    return Object.keys(componentConfig)
}


export default {
    registerWebComponent,
    registerWebComponents,
    getComponents
}